import JSONbig from 'json-bigint'
export default {
	// 微信支付
	Payment(payParams, callback) {
		uni.requestPayment({
			...payParams,
			success: function(res) {
				callback(1)
				console.log('支付成功');
			},
			fail: function(res) {
				callback(2)
				this.showToast('支付失败')
			}
		});
	},
	showLoading(text) {
		uni.showLoading({
			title: text
		});
	},
	// 弹框
	showToast(title) {
		uni.showToast({
			title: title,
			icon: 'none',
			duration: 3000,
		})
	},
	ifToken(callback) { //校验是否登录
		let token = uni.getStorageSync('Token');
		if (!token) {
			// this.showToast('请登录,请先登录!');
			uni.showModal({
				title: '未登陆',
				content: '请先登录后操作',
				confirmText: '去登陆',
				success: function(res) {
					if (res.confirm) {
						callback(1) //未登录进行未登录的操作
					}else{
						callback(3)
					}
				}
			});
			return
		} else {
			callback(2) //登录后
		}
	},
	ifTokens(callback) { //校验是否登录方式二
		let token = uni.getStorageSync('Token');
		if (!token) {
			callback(1) //未登录进行未登录的操作
		} else {
			callback(2) //登录后
		}
	},
	switchTab(url) {
		uni.switchTab({
			url: url
		})
	},
	ifPhone(text) { //手机号码校验
		if (text) {
			let mPattern = /^1[23456789]\d{9}$/;
			if (!mPattern.test(text)) {
				this.showToast('手机号码输入有误')
				return false
			} else {
				return true
			}
		} else {
			this.showToast('请输入手机号码')
			return false
		}
	},
	// 获取当前日期
	getNowFormatDate(callback) {
		var date = new Date();
		var seperator1 = "-";
		var year = date.getFullYear();
		var month = date.getMonth() + 1;
		var day = date.getDate();
		if (month >= 1 && month <= 9) {
			month = "0" + month;
		}
		if (day >= 0 && day <= 9) {
			day = "0" + day;
		}
		var currentdate = year + seperator1 + month + seperator1 + day;
		callback(currentdate);
	},
	getNowFormatDates(callback) { //获取明天日期
		var today = new Date();
		// 将日期加一天
		today.setDate(today.getDate() + 1);
		// 获取明天的日期
		var tomorrow = today.toISOString().split('T')[0];
		callback(tomorrow);
	},

	navigateTo(url) { //跳转页面
		uni.navigateTo({
			url: url
		})
	},
	makePhoneCall(phone) { //拨打电话
		uni.makePhoneCall({
			phoneNumber: phone,
			success: function() {}
		})
	},
	chooseLocation(callback) { //获取位置信息
		uni.chooseLocation({
			success: function(res) {
				callback(res)
				// that.address = res.address; //地址名称
				// that.latitude = res.latitude; //纬度
				// that.longitude = res.longitude; //经度
			},
			fail(e) {
				this.showToast('获取位置信息失败')
			}
		});
	},
	// 订阅消息
	subScribeMsg(i) {//callback
		let template = [
			'TvY_-xlaWjslKTSYlQcRILVio2QXcI2xsIrgUHPGSAE', //报名消息模板id
			'pE7iYzhi2iX9pxVyInbV6YSefEOnQ4CjYWsuAiLScRA', //审核结果模板id
		]
		var that = this
		uni.getSetting({
			withSubscriptions: true,
			success(res) {
				if (!res.subscriptionsSetting.mainSwitch) {
					uni.openSetting({ //打开设置页
						success(res) {
							console.log('打开设置页', res.authSetting);
						}
					})
				} else {
					uni.requestSubscribeMessage({
						// tmplIds: [template[i]],
						tmplIds: template,
						success(res) {
							console.log('requestSubscribeMessage 订阅信息', res);
							// callback(1)
							// if (template == "accept") { // 用户点击确定后
							// 	console.log('用户订阅点击确定按钮');
							// 	// that.getSubMsg()
							// } else {
							// 	console.log('拒绝');
							// }
						},
						fail(errMessage) {
							// callback(2)
							console.log("订阅消息 失败 ", errMessage);
						},
						// 不管成功或失败都要执行
						complete() {
							// callback(3)
						}
					})
				}
			},
		})
	},




	sm(msg, type, callback) {
		const that = this
		// 暂时关闭解密
		// msg 传过来的数据
		const sm2 = require('sm-crypto').sm2
		let publicKey =
			'04F90BBD5DB2E5B1BC6AFC09B256851915DA4963C37DFCB88A774D5A9E600404ADC3A81B2145AD86370B4A0D668B79EFAACEC2C5DCB96DE45636BCD4288D603966' // 公钥
		let privateKey = '583399904D456374199136686D0AC8BEBF4087CB32A2B26D0C9DFDF618F65D3B' // 私钥
		if (type == 1) { //加密
			console.log('请求参数', msg)
			let data = sm2.doEncrypt(JSON.stringify(msg), publicKey) //进行加密
			callback(data) //加密后得数据
		} else if (type == 2) { // 解密
			// console.log(msg, '要解密参数')
			if (msg.data.code != undefined || msg.code != undefined) {
				if (msg.data.code == 0) {
					callback(msg.data.data)
				} else {
					that.showToast(msg.data.msg)
				}
			} else {
				let data = sm2.doDecrypt(msg.data.data, privateKey) //进行
				console.log('接口返回的结果', JSONbig.parse(data))
				let res = JSONbig.parse(data)
				uni.hideLoading()
				if (res.code == 0 || res.code == 60000) {
					callback(res)
				} else {
					this.showToast(res.msg)
				}
			}
		} else {
			return
		}
	}
}